<?php
/**
 * 博客系统工具类库
 */
class BlogTool {

    // 获取客户端操作系统
    public static function getOsSystem()
    {
        if(!empty($_SERVER['HTTP_USER_AGENT']))
        {
            $OS = $_SERVER['HTTP_USER_AGENT'];
            if (preg_match('/win/i',$OS))
            {
                $OS = 'Windows';
            }
            elseif (preg_match('/mac/i',$OS))
            {
                $OS = 'MAC';
            }
            elseif (preg_match('/linux/i',$OS))
            {
                $OS = 'Linux';
            }
            elseif (preg_match('/unix/i',$OS))
            {
                $OS = 'Unix';
            }
            elseif (preg_match('/bsd/i',$OS))
            {
                $OS = 'BSD';
            }
            else
            {
                $OS = 'Other';
            }
            return $OS;
        }
        else
            return "unknown";
    }

    // 获取mysql版本
    public static function getMysqlVersion()
    {
        return Yii::app()->db->getServerVersion();
    }

    // 成功跳转
    public static function successRedirect($message, $url='')
    {

        $controller = Yii::app()->controller;

        if($message == null && is_array($url))
            $controller->redirect($url);

        $redirect = ($url != '' ?
            array('public/success', 'msg'=>$message, 'url'=>$url):
            array('public/success', 'msg'=>$message));

        $controller->redirect($redirect);
    }

    // 截取过长字符串
    public static function cutText($string, $len, $end='...')
    {
        if(mb_strlen($string, 'utf-8') > $len)
            $string = mb_substr($string, 0, $len, 'utf-8').$end;

        return $string;
    }

    // 缩略图函数
    public static function getThumb($maxWidth, $maxHeight, $name, $fileType)
    {
        $im = null;

        switch($fileType)
        {
            case 'jpg':
            case 'pjpeg':
                $im = imagecreatefromjpeg($name);
                break;
            case 'gif':
                $im = imagecreatefromgif($name);
                break;
            case 'png':
            case 'x-png':
                $im = imagecreatefrompng($name);
                break;
            default:
                break;
        }

        CThumb::resizeImage($im, $maxWidth, $maxHeight, $name, $fileType);
    }
}